﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Table_Dal;
using BLL;
using System.Collections;

namespace ApplicationLayer
{
    public partial class frmRaDeGiaiThuat : DevComponents.DotNetBar.OfficeForm
    {
        public frmRaDeGiaiThuat()
        {
            InitializeComponent();
        }
        string user ="";
        string TenDeThi = "";
        string Thoigian = "";
        int SocauHoi = 0;
        Chuong_GiaiThuatTbl chg_gt;
        MonHoc_GiaiThuatTbl mon_gt;
        YeuCauDeThi_GiaiThuatTbl dethi_gt;
        List<Chuong_GiaiThuatTbl> lstChuong;
        bool giaithuat = false;
        public frmRaDeGiaiThuat(string user, string tendethi, string ThoiGian, int Socauhoi)
        {
            
           this.user = user;
           TenDeThi = tendethi;
           Thoigian = ThoiGian;
           SocauHoi = Socauhoi;
            InitializeComponent();
        }
        List<ChuongTbl> DSChuong;
        private void frmRaDeGiaiThuat_Load(object sender, EventArgs e)
        {
            txtTenDe.Text = TenDeThi;
            numThoiGian.Value = int.Parse(Thoigian);
            numCauHoi.Value = SocauHoi;
            numThoiGian.Enabled = false;
            numCauHoi.Enabled = false;
            Load_cboMonHoc();
        }
        public void Load_cboMonHoc()
        {
            cboMonhoc.Items.Clear();
            List<MonHocTbl> DSMonHoc = new List<MonHocTbl>();
            MonHocBLL mh = new MonHocBLL();
            DSMonHoc = mh.LayTatCaMonHoc();
            int Dem = DSMonHoc.Count;
            if (Dem == 0) return;
            cboMonhoc.DataSource = DSMonHoc;
            cboMonhoc.DisplayMember = "TenMonHoc";
            cboMonhoc.ValueMember = "MaMonHoc";
            if (cboMonhoc.Items.Count > 0)
            {
                cboMonhoc.SelectedIndex = 0;
                indexCbo = 0;
            }
        }
        int  indexCbo=0;
        int count_chuongselect = 0;
        private void cboMonhoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (count_chuongselect > 0 && indexCbo != cboMonhoc.SelectedIndex)
                if (MessageBox.Show("Các chương đã chọn sẽ mất. Bạn chắc chắn chọn môn khác chứ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    load_lstboxChuong();
                    pnlChuong.Controls.Clear();
                    count_chuongselect = 0;
                    indexCbo = cboMonhoc.SelectedIndex;
                }
                else
                {
                    cboMonhoc.SelectedIndex = indexCbo;
                }
            else if (count_chuongselect > 0 && indexCbo == cboMonhoc.SelectedIndex)
                return;
            else
                load_lstboxChuong();
            indexCbo = cboMonhoc.SelectedIndex;
        }
        public void load_lstboxChuong()
        {
            if (cboMonhoc.DataSource == null || cboMonhoc.Items.Count == 0)
                return;
            lstboxChuong.DataSource = null;
            ChuongBll ch = new ChuongBll();
            DSChuong= new List<ChuongTbl>();
            MonHocBLL mh = new MonHocBLL();

            DSChuong = ch.DSChuong_MaMon(cboMonhoc.SelectedValue.ToString());
            int Dem = DSChuong.Count;
            if (Dem == 0)
            {
                lstboxChuong.Text = "";
                return;
            }
            lstboxChuong.DataSource = DSChuong;
            lstboxChuong.DisplayMember = "TenChuong";
            lstboxChuong.ValueMember = "MaChuong";
            if (DSChuong.Count > 0)
            {
                lstboxChuong.SelectedIndex = 0;
            }
            else
                lstboxChuong.Text = "";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (lstboxChuong.SelectedItems.Count == 0)
                return;
            for (int i = lstboxChuong.SelectedItems.Count-1; i>=0; i--)
            {
                TaoPanel((ChuongTbl)lstboxChuong.SelectedItems[i]);
                count_chuongselect++;
                DSChuong.Remove((ChuongTbl)lstboxChuong.SelectedItems[i]);
            }
            loadlstbox();
        }

        private void loadlstbox()
        {
            lstboxChuong.DataSource = null;
            lstboxChuong.Items.Clear();
            int Dem = DSChuong.Count;
            if (Dem == 0)
            {
                lstboxChuong.Text = "";
                return;
            }
            lstboxChuong.DataSource = DSChuong;
            lstboxChuong.DisplayMember = "TenChuong";
            lstboxChuong.ValueMember = "MaChuong";
            if (DSChuong.Count > 0)
            {
                lstboxChuong.SelectedIndex = 0;
            }
            else
                lstboxChuong.Text = "";
            //lstboxChuong.
        }
        private void TaoPanel(ChuongTbl chg)
        {
            ExpandablePanel epl = new ExpandablePanel();

            epl.Name = "pnl" + chg.MaChuong;
            if(chg.TenChuong.Length<=25)
            epl.TitleText = chg.TenChuong;
            else
                epl.TitleText = chg.TenChuong.Substring(0,25)+"...";
            epl.TitleStyle.Alignment = System.Drawing.StringAlignment.Center;
            epl.TitleStyle.BackColor1.ColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBackground;
            epl.TitleStyle.BackColor2.ColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBackground2;
            epl.TitleStyle.Border = DevComponents.DotNetBar.eBorderType.RaisedInner;
            epl.TitleStyle.BorderColor.ColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBorder;
            epl.TitleStyle.ForeColor.ColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelText;
            //epl.Style.Alignment = System.Drawing.StringAlignment.Center;
            //epl.Style.BackColor1.ColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBackground;
            //epl.Style.BackColor2.ColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBackground2;
            //epl.Style.Border = DevComponents.DotNetBar.eBorderType.SingleLine;
            //epl.Style.BorderColor.ColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.BarDockedBorder;
            //epl.Style.ForeColor.ColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.ItemText;
            epl.Style.GradientAngle = 90;

            epl.TitleStyle.GradientAngle = 90;

            LabelX lbl = new LabelX();
            lbl.Text = "Kiến thức: ";
            NumericUpDown num = new NumericUpDown();
            num.Name = "num" + chg.MaChuong;
            num.Value = 0;
            num.Tag = chg;
            LabelX lbl1 = new LabelX();
            lbl1.Text = "%";
            Button del = new Button();
            del.Name = "btnDel" + chg.MaChuong;
            del.Text = "Xóa";
            del.Tag = chg;
            del.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F);
            del.Click += del_Click;
            epl.Controls.Add(lbl);
            lbl.Location = new System.Drawing.Point(31, 29);
            lbl.Size = new System.Drawing.Size(75, 23);
            epl.Controls.Add(num);
            num.Location = new System.Drawing.Point(112, 29);
            num.Size = new System.Drawing.Size(65, 20);
            epl.Controls.Add(lbl1);
            lbl1.Location = new System.Drawing.Point(183, 29);
            lbl1.Size = new System.Drawing.Size(45, 23);

            epl.Controls.Add(del);
            del.Location = new System.Drawing.Point(243, 29);
            del.Size = new System.Drawing.Size(50, 23);
            pnlChuong.Controls.Add(epl);
            epl.Dock = DockStyle.Top;
            epl.Size = new Size(304, 57);
            
        }

        private void del_Click(object sender, EventArgs e)
        {
            Button x = (Button)sender;
            //MessageBox.Show(x.Name.Substring(6));
            pnlChuong.Controls.RemoveByKey("pnl" + x.Name.Substring(6));
            ChuongTbl chg = new ChuongTbl();
            chg =(ChuongTbl) x.Tag;
            DSChuong.Add(chg);
            count_chuongselect--;
            loadlstbox();
        }

        private void cboMonhoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void btnTaoDe_Click(object sender, EventArgs e)
        {
            int temp = 0;
            foreach (Control Pnl in pnlChuong.Controls)
            {
                foreach (Control item in Pnl.Controls)
                {
                    
                    if (item is NumericUpDown)
                    {
                        if (((NumericUpDown)item).Value < 0 || ((NumericUpDown)item).Value > 100)
                        {
                            item.Select();
                            errorProvider1.SetError(item, "Nhập sai");
                            return;
                        }
                        temp += (int)((NumericUpDown)item).Value;
                    }
                }
            }
            
            if (temp != 100)
            {
                MessageBox.Show("Chưa đủ 100% kiến thức môn học?", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            int k = (int)numRatKho.Value + (int)numKho.Value + (int)numVua.Value + (int)numDe.Value + (int)numRatDe.Value;
            if (k != 100)
            {
                numRatKho.Select();
                MessageBox.Show("Chưa đủ 100% độ khó", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            dethi_gt = new YeuCauDeThi_GiaiThuatTbl("0", txtTenDe.Text, (int)numThoiGian.Value, (int)numCauHoi.Value, (int)numRatKho.Value, (int)numKho.Value, (int)numVua.Value, (int)numDe.Value, (int)numRatDe.Value);
            mon_gt = new MonHoc_GiaiThuatTbl(cboMonhoc.SelectedValue.ToString(), cboMonhoc.SelectedText.ToString(), "0");
            lstChuong = new List<Chuong_GiaiThuatTbl>();
            foreach (Control Pnl in pnlChuong.Controls)
            {
                foreach (Control item in Pnl.Controls)
                {

                    if (item is NumericUpDown)
                    {
                        if ((int)((NumericUpDown)item).Value > 0 && (int)((NumericUpDown)item).Value <= 100)
                        {
                            ChuongTbl chg0 = new ChuongTbl();
                            chg0 = (ChuongTbl)item.Tag;
                            chg_gt = new Chuong_GiaiThuatTbl(chg0.MaChuong, chg0.TenChuong, (int)((NumericUpDown)item).Value, mon_gt.MaMonHoc);
                            lstChuong.Add(chg_gt);
                        }
                    }
                }
            }
            if (swbtnGiaiThuat.Value == true)
                giaithuat = true;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
        public YeuCauDeThi_GiaiThuatTbl DeThi_GT()
        {
            return dethi_gt;
        }
        public MonHoc_GiaiThuatTbl MonHoc_Gt()
        {
            return mon_gt;
        }
        public List<Chuong_GiaiThuatTbl> ArrlstChuong()
        {
            return lstChuong;
        }
        public bool GiaiThuat()
        {
            return giaithuat;
        }
        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }
}